﻿//********************************************************************************//
//创建日期：<创建日期，2016-11-08>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：增取水口
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using System.Text;

namespace Web.Pages.QuShui.DiXiaS.YongHuGL.HuJiGL
{
    public partial class ZengQuSK : BasePage
    {
        #region 属性
        /// <summary>
        /// gridView主键
        /// </summary>
        private int QUSHUIK_ID
        {
            get
            {
                return Convert.ToInt32(ViewState["_QUSHUIK_ID"]);
            }
            set
            {
                ViewState["_QUSHUIK_ID"] = value;
            }
        }
        /// <summary>
        /// 用户号
        /// </summary>
        private string YHH
        {
            get
            {
                return ViewState["_YHH"] as string;
            }
            set
            {
                ViewState["YHH"] = value;
            }
        }
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        QS_YG_YONGHUXXBLL _QS_YG_YONGHUXXBLL = new QS_YG_YONGHUXXBLL();
        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 取水口信息
        /// </summary>
        QS_YH_QUSHUIKXXB _QSKXX = new QS_YH_QUSHUIKXXB();

        /// <summary>
        /// 取水口信息BLL
        /// </summary>
        QS_YH_QUSHUIKXXBBLL _QSKXXBLL = new QS_YH_QUSHUIKXXBBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                Display();
                LoadData();
                InitData();

            }
        }

        /// <summary>
        /// 重写Render方法
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }


        /// <summary>
        /// 放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
            txtYongHuH.ClientEnabled = true;
            txtYongHuH.Focus();

        }

        /// <summary>
        /// 修改
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Add;
            Display();
            if (txtYongHuH.Text.Trim() == "")
            {
                txtYongHuH.ClientEnabled = true;
                txtYongHuH.Focus();
            }
            else
            {
                txtYongHuH.ClientEnabled = false;
                txtQuShuiKBH.Focus();
            }

        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }

        /// <summary>
        /// 前台事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            string yhh = this.hdCollection.Get("yhh").ToString();

            if (e.Parameter == "setinfo")
            {
                string msg = "";

                if (yhh != "")
                {
                    DataTable dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND S_YONGHUH='{0}'", yhh), null);
                    msg = dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
                }
                e.Result = e.Parameter + "|0|" + msg;
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //txtYongHuH.Text = "";
            //txtYONGHUM.Text = "";
            txtQuShuiKBH.Text = "";
            txtYuanShenJBH.Text = "";
            txtQuShuiDD.Text = "";
            txtTuiShuiWZ.Text = "";
            cmbXingZhengQ.Text = "";
            cmbLiuYu.Text = "";
            cmbShuiZiYFQ.Text = "";
            cmbShuiLiFP.Text = "";
            cmbHuanXianBJ.Text = "";
            cmbQuShuiLX.Text = "";
            txtQuShuiKM.Text = "";
            txtDu.Text = "";
            txtFen.Text = "";
            txtMiao.Text = "";
            txtDu2.Text = "";
            txtFen2.Text = "";
            txtMiao2.Text = "";
            txtQuShuiSY.Text = "";
            cmbQuShuiFS.Text = "";
            cmbHuanXianBJ.Text = "";
            cmbShuiYuanSZ.Text = "";
            memoSYSZBZ.Text = "";
            memoBeiZhu.Text = "";
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            PageTools.QSLoadCombox(cmbXingZhengQ, PageTools.eLoadType.行政区QS, null, "");
            PageTools.QSLoadCombox(cmbHuanXianBJ, PageTools.eLoadType.环线标记QS, null, "");

            //PageTools.QSLoadCombox(cmbQuShuiLX, PageTools.eLoadType., null, "");
            //PageTools.QSLoadCombox(cmbQuShuiFS, PageTools.eLoadType., null, "");
            //PageTools.QSLoadCombox(cmbShuiYuanSZ, PageTools.eLoadType., null, "");

            PageTools.QSLoadCombox(cmbLiuYu, PageTools.eLoadType.流域QS, null, "");
            PageTools.QSLoadCombox(cmbShuiZiYFQ, PageTools.eLoadType.水资源分区QS, null, "");
            PageTools.QSLoadCombox(cmbShuiLiFP, PageTools.eLoadType.水利分片QS, null, "");
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            string yonghuh = this.Request.QueryString["yonghuh"];
            if (yonghuh != "" && yonghuh != null)
            {
                this.YHH = yonghuh;
                txtYongHuH.Text = yonghuh;
                txtYongHuH.ClientEnabled = false;
                DataTable dt = _QS_YG_YONGHUXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 AND S_YONGHUH='{0}'", yonghuh), null);
                txtYONGHUM.Text = dt.Rows.Count > 0 ? dt.Rows[0]["S_YONGHUM"].ToString() : "";
            }
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnEdit.Visible = true;
                    btnEdit.Enabled = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    txtQuShuiKBH.ClientEnabled = false;
                    SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnEdit.Visible = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    InitControl();
                    SetText(true);
                    break;
            }
        }


        /// <summary>
        /// 设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            txtQuShuiKBH.ClientEnabled = b;
            txtYuanShenJBH.ClientEnabled = b;
            txtQuShuiDD.ClientEnabled = b;
            txtTuiShuiWZ.ClientEnabled = b;
            cmbXingZhengQ.ClientEnabled = b;
            cmbLiuYu.ClientEnabled = b;
            cmbShuiZiYFQ.ClientEnabled = b;
            cmbShuiLiFP.ClientEnabled = b;
            cmbQuShuiLX.ClientEnabled = b;
            txtQuShuiKM.ClientEnabled = b;
            txtDu.ClientEnabled = b;
            txtFen.ClientEnabled = b;
            txtMiao.ClientEnabled = b;
            txtDu2.ClientEnabled = b;
            txtFen2.ClientEnabled = b;
            txtMiao2.ClientEnabled = b;
            txtQuShuiSY.ClientEnabled = b;
            cmbQuShuiFS.ClientEnabled = b;
            cmbHuanXianBJ.ClientEnabled = b;
            cmbShuiYuanSZ.ClientEnabled = b;
            memoSYSZBZ.ClientEnabled = b;
            memoBeiZhu.ClientEnabled = b;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            int wtype = 1;
            int slym = BusinessTool.GetSLYMorZWNY(1, wtype);
            int userid = LoginInfo.CurrentUser.UserID;
            OperateState operateState;
            //修改操作
            operateState = OperateState.Add;

            DataTable dtjh = _QSKXXBLL.SelectQS_YH_QUSHUIKXXBsDynamic(string.Format("i_jlzt=0 and S_QUSHUIKBH='{0}'", txtQuShuiKBH.Text), null);
            if (dtjh.Rows.Count > 0)
            {
                PageTools.AlertMsg(this, "此取水口编号已经被使用，请重新填写！");
                this.txtQuShuiKBH.Focus();
            }
            else
            {
                _QSKXX.ID = 0;
                _QSKXX.I_JLZT = 0;
                _QSKXX.S_QUSHUIKPH = txtYuanShenJBH.Text;
                _QSKXX.S_QUSHUIKBH = txtQuShuiKBH.Text;
                _QSKXX.S_QUSHUIDZ = txtQuShuiDD.Text;
                _QSKXX.S_TUISHUIWZ = txtTuiShuiWZ.Text;
                _QSKXX.S_XINGZHENGQBH = cmbXingZhengQ.Text != "" ? cmbXingZhengQ.Value.ToString() : "";
                _QSKXX.S_LIUYUBH = cmbLiuYu.Text != "" ? cmbLiuYu.Value.ToString() : "";
                _QSKXX.S_SHUIZIYBH = cmbShuiZiYFQ.Text != "" ? cmbShuiZiYFQ.Value.ToString() : "";
                _QSKXX.S_FENGPIANBH = cmbShuiLiFP.Text != "" ? cmbShuiLiFP.Value.ToString() : "";
                _QSKXX.S_HUANGXIANBH = cmbHuanXianBJ.Text != "" ? cmbHuanXianBJ.Value.ToString() : "";
                _QSKXX.S_QUSHUIKM = txtQuShuiKM.Text;
                _QSKXX.S_QUSHUISY = txtQuShuiSY.Text;
                string zb = txtDu.Text + "/" + txtFen.Text + "/" + txtMiao.Text + "/" + txtDu2.Text + "/" + txtFen2.Text + "/" + txtMiao2.Text;
                _QSKXX.S_ZUOBIAO = zb;
                _QSKXX.S_QUSHUIFS = cmbQuShuiFS.Text != "" ? cmbQuShuiFS.Value.ToString() : "";
                _QSKXX.S_SHUIYUANSZ = cmbShuiYuanSZ.Text != "" ? cmbShuiYuanSZ.Value.ToString() : "";
                _QSKXX.S_SHUIYUANSZBZ = memoSYSZBZ.Text;
                _QSKXX.S_BEIZHU = memoBeiZhu.Text;
                _QSKXX.S_KAISHINY = slym.ToString();
                _QSKXX.S_CAOZUOR = userid.ToString();
                _QSKXX.D_CAOZUORQ = DateTime.Now;
                _QSKXX.S_TIANJIAR = userid.ToString();
                _QSKXX.D_TIANJIARQ = DateTime.Now;
                _SYS_LOGBLL.Log<QS_YH_QUSHUIKXXB>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-增取水口", operateState, _QSKXX, null);
                bool result = _QSKXXBLL.InsertOrUpdate(_QSKXX);
                //用户水井关系表也要更新数据
                string v = "";
                _spHelper.P_QSYH_ZJORZB(wtype, 3, userid.ToString(), txtYongHuH.Text, txtQuShuiKBH.Text, out v);
                if (result && v == "1")
                {
                    
                    PageTools.AlertMsg(this, "保存成功！");
                    this.Status = GridViewStatus.Init;
                    Display();
                    txtYongHuH.ClientEnabled = true;
                    txtYongHuH.Focus();
                }
                else
                {
                    PageTools.AlertMsg(this, "保存失败！");
                }
            }
        }

     
        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (this.txtQuShuiKBH.Text == "")
            {
                PageTools.AlertMsg(this, "取水口编号不能为空！");
                this.txtQuShuiKBH.Focus();
                return false;
            }
            if (this.txtQuShuiKBH.Text.Length != 7 || !PageTools.ValidateString(this.txtQuShuiKBH.Text, PageTools.eValidateType.数字))
            {
                PageTools.AlertMsg(this, "取水口编号应为7位数字！");
                this.txtQuShuiKBH.Focus();
                return false;
            }
            if (this.txtQuShuiDD.Text == "")
            {
                PageTools.AlertMsg(this, "取水地址不能为空！");
                this.txtQuShuiDD.Focus();
                return false;
            }
            if (this.txtTuiShuiWZ.Text == "")
            {
                PageTools.AlertMsg(this, "退水位置不能为空！");
                this.txtTuiShuiWZ.Focus();
                return false;
            }
            if (this.cmbXingZhengQ.Text == "")
            {
                PageTools.AlertMsg(this, "行政区不能为空！");
                this.cmbXingZhengQ.Focus();
                return false;
            }
            if (this.cmbLiuYu.Text == "")
            {
                PageTools.AlertMsg(this, "流域不能为空！");
                this.cmbLiuYu.Focus();
                return false;
            }
            if (this.cmbShuiZiYFQ.Text == "")
            {
                PageTools.AlertMsg(this, "水资源分区不能为空！");
                this.cmbShuiZiYFQ.Focus();
                return false;
            }
            if (this.cmbShuiLiFP.Text == "")
            {
                PageTools.AlertMsg(this, "水利分片不能为空！");
                this.cmbShuiLiFP.Focus();
                return false;
            }
            if (this.cmbQuShuiLX.Text == "")
            {
                PageTools.AlertMsg(this, "取水类型不能为空！");
                this.cmbQuShuiLX.Focus();
                return false;
            }
            if (this.txtQuShuiKM.Text == "")
            {
                PageTools.AlertMsg(this, "取水口名不能为空！");
                this.txtQuShuiKM.Focus();
                return false;
            }
            if (this.txtQuShuiSY.Text == "")
            {
                PageTools.AlertMsg(this, "取水水源不能为空！");
                this.txtQuShuiSY.Focus();
                return false;
            }
            if (this.cmbQuShuiFS.Text == "")
            {
                PageTools.AlertMsg(this, "取水方式不能为空！");
                this.cmbQuShuiFS.Focus();
                return false;
            }
            if (this.cmbShuiYuanSZ.Text == "")
            {
                PageTools.AlertMsg(this, "水源水质不能为空！");
                this.cmbShuiYuanSZ.Focus();
                return false;
            }
            return true;
        }

        #endregion
    }
}